package com.yanqu.road.server.channelserver.cmd.activity.entername;

import com.yanqu.road.entity.activity.entername.ActivityGroupMatch;
import com.yanqu.road.entity.activity.entername.UserEnterName;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.activity.EnterNameProto;
import com.yanqu.road.server.channelserver.cmd.ChannelCommand;
import com.yanqu.road.server.channelserver.logic.ChannelMessageHelper;
import com.yanqu.road.server.channelserver.manager.activity.entername.ChannelEnterNameMgr;
import com.yanqu.road.server.channelserver.manager.activity.entername.ChannelEnterNameRankMgr;
import com.yanqu.road.server.channelserver.manager.activity.entername.EnterNameActivity;
import com.yanqu.road.server.channelserver.manager.activity.entername.EnterNameModel;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameGroupRankModel;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = ChannelProtocol.N_ENTER_NAME_GROUP_RANK, desc = "地区排行")
public class ChannelEnterNameGroupRankCmd extends ChannelCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        EnterNameProto.ChannelEnterNameGroupRankReqMsg reqMsg = EnterNameProto.ChannelEnterNameGroupRankReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        int type = 0;
        if(reqMsg.hasType()){
            type = reqMsg.getType();
        }
        boolean allGroup = false;
        if(reqMsg.hasAllGroup()){
            allGroup = reqMsg.getAllGroup();
        }
        if(type == 0){
            EnterNameActivity activity = ChannelEnterNameMgr.getEnterNameActivity(activityId);
            if(activity != null){
                if(allGroup){
                    type = activity.getAllGroupConditionType();
                }else {
                    type = activity.getGroupConditionType();
                }
            }
        }
        if(type == 0){
            //默认牧场点兵
            type = eGamePlayerEventType.EnterNameAreaFightRank.getValue();
        }
        EnterNameProto.EnterNameGroupRankRespMsg.Builder clientMsg = EnterNameProto.EnterNameGroupRankRespMsg.newBuilder();
        clientMsg.setRet(0);
        clientMsg.setMyRank(-1);
        clientMsg.setMyValue("0");
        EnterNameModel model = ChannelEnterNameMgr.getEnterNameModelMap().get(activityId);
        if(model != null) {
            //拿分组信息
            UserEnterName userEnterName = model.getUserEnterName(reqMsg.getUserKey().getUserId());
            if(userEnterName != null) {
                EnterNameProto.EnterNameGroupRankListMsg.Builder listMsg = EnterNameProto.EnterNameGroupRankListMsg.newBuilder();
                ActivityGroupMatch groupMatch = model.getActivityGroupMatch(userEnterName.getGroupId());
                int groupMatchId = -1;
                if (groupMatch != null) {
                    groupMatchId = groupMatch.getRankMatchGroupId();
                }
                if(reqMsg.hasAllGroup() && reqMsg.getAllGroup()){
                    groupMatchId = 0;
                }
                if(groupMatchId >= 0) {
                    EnterNameGroupRankModel rankModel = ChannelEnterNameRankMgr.getEnterNameGroupRankModel(activityId, type, groupMatchId);
                    if (rankModel != null) {
                        if(rankModel.getListMsg() != null) {
                            for (int i = 0; i < rankModel.getListMsg().getGroupList().size(); i++) {
                                EnterNameProto.EnterNameGroupRankMsg rankMsg = rankModel.getListMsg().getGroup(i);
                                EnterNameProto.EnterNameGroupRankMsg.Builder builder = EnterNameProto.EnterNameGroupRankMsg.newBuilder();
                                builder.setGroupId(rankMsg.getGroupId());
                                builder.setValue(rankMsg.getValue());
                                listMsg.addGroup(builder);
                                if (userEnterName.getGroupId() == rankMsg.getGroupId()) {
                                    clientMsg.setMyRank(i + 1);
                                    clientMsg.setMyValue(rankMsg.getValue());
                                }
                            }
                        }
                    }
                }
                clientMsg.setList(listMsg);
            }else {
                ChannelEnterNameRankMgr.getLogger().info("activityId {}, user {}  UserEnterName null", activityId, reqMsg.getUserKey().getUserId());
            }
        }else {
            ChannelEnterNameRankMgr.getLogger().info("activityId {}, group EnterNameModel null", activityId);
        }

        ChannelMessageHelper.sendGamePacket(Protocol.U_ENTER_NAME_GROUP_RANK, clientMsg, reqMsg.getUserKey().getChannelId(),
                reqMsg.getUserKey().getServerId(), reqMsg.getUserKey().getUserId());
    }

    @Override
    public void sendData() {

    }
}
